package cn.s1995.sloth.bms.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cn.s1995.sloth.common.core.util.R;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import cn.s1995.sloth.common.log.annotation.SysLog;
import cn.s1995.sloth.bizs.entity.UserFeedback;
import cn.s1995.sloth.bizs.service.UserFeedbackService;
import org.jetbrains.annotations.NotNull;
import org.springframework.security.access.prepost.PreAuthorize;
import cn.s1995.sloth.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 用户反馈
 *
 * @author Lex
 * @date 2023-12-28 01:20:24
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/userFeedback")
@Tag(description = "UserFeedback", name = "BMS-用户反馈管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class UserFeedbackController {

    private final UserFeedbackService userFeedbackService;

    /**
     * 获取分页
     * @param page 分页对象
     * @param req 用户反馈
     * @return
     */
    @ApiOperationSupport(order = 1)
    @Operation(summary = "获取分页", description = "获取分页")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('bizs_UserFeedback_view')")
    public R getPage(@ParameterObject Page page, @ParameterObject UserFeedback req) {
        LambdaQueryWrapper<UserFeedback> wrapper = buildQueryWrapper(req);

        return R.ok(userFeedbackService.page(page, wrapper));
    }

    /**
     * 获取列表
     * @param req 用户反馈
     * @return
     */
    @ApiOperationSupport(order = 1)
    @Operation(summary = "获取列表", description = "获取列表")
    @GetMapping("/list")
    @PreAuthorize("@pms.hasPermission('bizs_UserFeedback_view')")
    public R getList(@ParameterObject UserFeedback req) {
        return R.ok(userFeedbackService.list(buildQueryWrapper(req)));
    }

    /**
     * 获取对象
     * @param id id
     * @return R
     */
    @ApiOperationSupport(order = 1)
    @Operation(summary = "获取对象", description = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('bizs_UserFeedback_view')")
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(userFeedbackService.getById(id));
    }

    /**
     * 新增用户反馈
     * @param req 用户反馈
     * @return R
     */
    @ApiOperationSupport(order = 10)
    @Operation(summary = "新增用户反馈", description = "新增用户反馈")
    @SysLog("新增用户反馈")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('bizs_UserFeedback_add')")
    public R save(@RequestBody UserFeedback req) {
        return R.ok(userFeedbackService.save(req));
    }

    /**
     * 修改用户反馈
     * @param req 用户反馈
     * @return R
     */
    @ApiOperationSupport(order = 20)
    @Operation(summary = "修改用户反馈", description = "修改用户反馈")
    @SysLog("修改用户反馈")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('bizs_UserFeedback_edit')")
    public R updateById(@RequestBody UserFeedback req) {
        return R.ok(userFeedbackService.updateById(req));
    }

    /**
     * 通过id删除用户反馈
     * @param ids id列表
     * @return R
     */
    @ApiOperationSupport(order = 30)
    @Operation(summary = "通过id删除用户反馈", description = "通过id删除用户反馈")
    @SysLog("通过id删除用户反馈")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('bizs_UserFeedback_del')")
    public R remove(@RequestBody Long[] ids) {
        return R.ok(userFeedbackService.removeBatchByIds(CollUtil.toList(ids)));
    }

    /**
     * 导出excel 表格
     * @param req 查询条件
     * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ApiOperationSupport(order = 40)
    @Operation(summary = "导出excel表格", description = "")
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('bizs_UserFeedback_export')")
    public List<UserFeedback> export(UserFeedback req, Long[] ids) {
        return userFeedbackService.list(Wrappers.lambdaQuery(req).in(ArrayUtil.isNotEmpty(ids), UserFeedback::getId, ids));
    }

    @NotNull
    private static LambdaQueryWrapper<UserFeedback> buildQueryWrapper(UserFeedback req) {
        LambdaQueryWrapper<UserFeedback> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(Objects.nonNull(req.getType()), UserFeedback::getType, req.getType());
        wrapper.eq(Objects.nonNull(req.getSource()), UserFeedback::getSource, req.getSource());
        wrapper.and(
                StrUtil.isNotBlank(req.getSearch()),
                i -> i.like(UserFeedback::getEmail, req.getSearch())
                        .or()
                        .like(UserFeedback::getTitle, req.getSearch())
                        .like(UserFeedback::getContent, req.getSearch())
        );
        return wrapper;
    }

}
